package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.goods.BrandList;
import com.cskaoyan.bean.goods.CategoryList;
import com.cskaoyan.bean.goods.GoodsCatAndBrand;
import com.cskaoyan.bean.goods.GoodsData;
import com.cskaoyan.mapper.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author xudengfeng
 * @date 2020/12/5 20:39
 */
@Service
public class AdminGoodsServiceImpl implements AdminGoodsService {
    @Autowired
    CategoryMapper categoryMapper;

    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    GoodsAttributeMapper goodsAttributeMapper;

    @Autowired
    GoodsSpecMapper goodsSpecMapper;

    @Autowired
    GoodsProductMapper goodsProductMapper;

    @Autowired
    BrandMapper brandMapper;

    @Value("${filter.deleted}")
    Boolean notDeleted;

    @Override
    public ListData<Goods> queryGoods(Integer page, Integer limit, String sort, String order, String goodsSn, String name) {
        //page页码
        //limit 当前页的大小
        PageHelper.startPage(page, limit);//修改了我们预编译的sql语句

        GoodsExample goodsExample = new GoodsExample();
        goodsExample.setOrderByClause(sort + " " + order);

        GoodsExample.Criteria criteria = goodsExample.createCriteria();
        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (goodsSn != null && !"".equals(goodsSn)) {
            criteria.andGoodsSnEqualTo(goodsSn);
        }
        //过滤掉已经逻辑删除了的商品
        criteria.andDeletedEqualTo(notDeleted);

        List<Goods> goodsList = goodsMapper.selectByExample(goodsExample);
        ListData<Goods> listData = new ListData<>();
        listData.setItems(goodsList);
        PageInfo<Goods> goodsPageInfo = new PageInfo<>(goodsList);
        long total = goodsPageInfo.getTotal();//没有分页的情况下，总的数据量
        listData.setTotal((int) total);
        return listData;
    }

    //逻辑删除
    @Override
    public int deleteGoodsById(Goods goods) {
        goods.setDeleted(true);
        return goodsMapper.updateByPrimaryKeySelective(goods);
    }

    @Override
    public GoodsData queryGoodsData(Integer id) {
        GoodsData goodsData = new GoodsData();

        //查出categories数组
        Map<String, Integer> map = categoryMapper.selectCategoryIds(id);
        ArrayList<Integer> categoryIds = new ArrayList<>();

        for (String key : map.keySet()) {
            Integer value = map.get(key);
            categoryIds.add(value);
        }
        //查出goods所属对象
        Goods goods = goodsMapper.selectByPrimaryKey(id);

        //查出attiribute对象数组
        GoodsAttributeExample goodsAttributeExample = new GoodsAttributeExample();
        GoodsAttributeExample.Criteria attributeCriteria = goodsAttributeExample.createCriteria();
        attributeCriteria.andDeletedEqualTo(notDeleted);

        //查出spec对象数组
        GoodsSpecExample goodsSpecExample = new GoodsSpecExample();
        GoodsSpecExample.Criteria specCriteria = goodsSpecExample.createCriteria();
        specCriteria.andDeletedEqualTo(notDeleted);

        //查出product对象数组
        GoodsProductExample goodsProductExample = new GoodsProductExample();
        GoodsProductExample.Criteria productCriteria = goodsProductExample.createCriteria();
        productCriteria.andDeletedEqualTo(notDeleted);

        if (id != null) {
            specCriteria.andGoodsIdEqualTo(id);
            productCriteria.andGoodsIdEqualTo(id);
            attributeCriteria.andGoodsIdEqualTo(id);
        }

        List<GoodsAttribute> goodsAttributes = goodsAttributeMapper.selectByExample(goodsAttributeExample);
        List<GoodsSpec> goodsSpecs = goodsSpecMapper.selectByExample(goodsSpecExample);
        List<GoodsProduct> goodsProducts = goodsProductMapper.selectByExample(goodsProductExample);

        goodsData.setCategoryIds(categoryIds);
        goodsData.setGoods(goods);
        goodsData.setAttributes(goodsAttributes);
        goodsData.setSpecifications(goodsSpecs);
        goodsData.setProducts(goodsProducts);

        return goodsData;
    }

    @Override
    public GoodsCatAndBrand queryCatAndBrand() {

        GoodsCatAndBrand goodsCatAndBrand = new GoodsCatAndBrand();


        //父分类下为L1
        String level = "L1";
        //分类
        List<CategoryList> categoryList = categoryMapper.selectValueAndLabelAndChildren(level, notDeleted);

        //供应商
        List<BrandList> brandList = brandMapper.selectValueAndLabel(notDeleted);


        goodsCatAndBrand.setCategoryList(categoryList);
        goodsCatAndBrand.setBrandList(brandList);
        return goodsCatAndBrand;
    }

    @Override
    public void updateGoodsInfo(GoodsData goodsData) {
        //修改goods表
        Goods goods = goodsData.getGoods();

        //修改时间
        Date date = new Date();

        Integer goodsId = goods.getId();
        goodsMapper.updateByPrimaryKey(goods);

        List<GoodsSpec> specifications = goodsData.getSpecifications();

        GoodsSpecExample goodsSpecExample = new GoodsSpecExample();
        GoodsSpecExample.Criteria criteria = goodsSpecExample.createCriteria();
        criteria.andGoodsIdEqualTo(goodsId);
        //找出该商品的所有规格信息
        List<GoodsSpec> goodsSpecs = goodsSpecMapper.selectByExample(goodsSpecExample);

        //分两步，第一、将所有规格信息删除 二、添加上传的规格信息

        for (GoodsSpec goodsSpec : goodsSpecs) {
            goodsSpec.setDeleted(true);
            goodsSpecMapper.updateByPrimaryKeySelective(goodsSpec);
        }
        for (GoodsSpec specification : specifications) {
            if (specification.getId() == null) {
                specification.setDeleted(false);
                specification.setGoodsId(goodsId);
                specification.setAddTime(date);
                specification.setUpdateTime(date);
                specification.setDeleted(false);
                goodsSpecMapper.insertSelective(specification);
            } else {
                goodsSpecMapper.updateByPrimaryKeySelective(specification);
            }
        }

        List<GoodsProduct> products = goodsData.getProducts();
        for (GoodsProduct product : products) {
            product.setUrl(goods.getPicUrl());
            goodsProductMapper.updateByPrimaryKey(product);
        }

        List<GoodsAttribute> attributes = goodsData.getAttributes();

        GoodsAttributeExample example = new GoodsAttributeExample();
        GoodsAttributeExample.Criteria attributeCriteria = example.createCriteria();
        attributeCriteria.andGoodsIdEqualTo(goodsId);
        List<GoodsAttribute> goodsAttributes = goodsAttributeMapper.selectByExample(example);

        for (GoodsAttribute goodsAttribute : goodsAttributes) {
            goodsAttributeMapper.deleteByPrimaryKey(goodsAttribute.getId());
        }
        for (GoodsAttribute attribute : attributes) {
            //当前端先删除，再添加 或 新增 时 需要额外塞入数据
            if (attribute.getId() == null) {
                attribute.setDeleted(false);
                attribute.setUpdateTime(date);
                attribute.setAddTime(date);
                attribute.setGoodsId(goodsId);
                goodsAttributeMapper.insertSelective(attribute);
            } else {
                goodsAttributeMapper.updateByPrimaryKeySelective(attribute);
            }
        }
    }

    @Override
    public void createGoods(GoodsData goodsData) {
        //插入表中的时间
        Date date = new Date();

        //插入商品表
        Goods goods = goodsData.getGoods();
        goods.setAddTime(date);
        goods.setUpdateTime(date);
        goods.setDeleted(false);
        goodsMapper.insertSelective(goods);

        //插入规格表
        List<GoodsSpec> specifications = goodsData.getSpecifications();
        for (GoodsSpec specification : specifications) {
            specification.setGoodsId(goods.getId());
            specification.setAddTime(date);
            specification.setUpdateTime(date);
            specification.setDeleted(false);
            goodsSpecMapper.insertSelective(specification);
        }
        List<GoodsProduct> products = goodsData.getProducts();
        for (GoodsProduct product : products) {
            product.setGoodsId(goods.getId());
            product.setAddTime(date);
            product.setUpdateTime(date);
            product.setDeleted(false);
            goodsProductMapper.insertSelective(product);
        }
        List<GoodsAttribute> attributes = goodsData.getAttributes();
        for (GoodsAttribute attribute : attributes) {
            attribute.setGoodsId(goods.getId());
            attribute.setAddTime(date);
            attribute.setUpdateTime(date);
            attribute.setDeleted(false);
            goodsAttributeMapper.insertSelective(attribute);
        }

    }
}



